import fetch from 'common/src/utils/fetch'
import UTILS from "common/src/utils/utils";

/**
 * 上传图片
 * @param data
 */
export function uploadimage(data) {
  return fetch({
    url: '/api/commonservice/obsfile/uploadimage',
    method: 'post',
    data
  });
}

/**
 * 上传文件
 * @param data
 */
export function uploadfile(data) {
  return fetch({
    url: '/api/commonservice/obsfile/uploadfile',
    method: 'post',
    data
  });
}

/**
 * 获取图片信息
 * @param key
 */
export function downloadimage(key) {
  return fetch({
    url: '/api/commonservice/obsfile/downloadimage?objectkey=' + key,
    method: 'GET'
  })
}

/**
 * 文件下载
 * @param key
 */
export function downloadfile(key) {
  let url = '/api/commonservice/obsfile/downloadfile?objectkey=' + key;
  window.open(encodeURI(url));
}

/* 
    带文件名下载
*/
export function downTemplate(fileName, url){
  return fetch({
    url: url,
    method: 'get',
    responseType: "blob",
    timeout: 0
  }).then(res => {
    UTILS.downloadFile(
      res,
      fileName,
      fileName.substr(fileName.lastIndexOf(".") + 1)
    );
  });
};
